package org.xxpay.pay.channel.ymkjpay;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.util.PayDigestUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

@Component
public class YmkjpayConfig {
    private static final MyLog _log = MyLog.getLog(YmkjpayConfig.class);
    private static String encodingCharset = "UTF-8";

    public static final String CHANNEL_NAME = "ymkjpay";
    public static final String CHANNEL_NAME_WECHAT = CHANNEL_NAME+"_wxpay_qr";
    public static final String CHANNEL_NAME_WECHAT_H5 = CHANNEL_NAME+"_wxpay_h5";
    public static final String CHANNEL_NAME_ALIPAY = CHANNEL_NAME+"_alipay_qr";
    public static final String CHANNEL_NAME_ALIPAY_H5 = CHANNEL_NAME+"_alipay_h5";
    public static final String CHANNEL_TYPE_WECHAT = "wechat";
    public static final String CHANNEL_TYPE_ALIPAY = "alipay";
    public static final String RETURN_VALUE_SUCCESS = "success";
    public static final String RETURN_VALUE_FAIL = "fail";

    // 商户ID
    private String mchId;
    // 商户Key
    private String key;
    // 请求地址
    private String reqUrl;


    public YmkjpayConfig(){}

    public YmkjpayConfig(String payParam) {
        Assert.notNull(payParam, "init ymkjpay config error");
        JSONObject object = JSONObject.parseObject(payParam);
        this.mchId = object.getString("mchId");
        this.key = object.getString("key");
        this.reqUrl = object.getString("reqUrl");

    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getReqUrl() {
        return reqUrl;
    }

    public void setReqUrl(String reqUrl) {
        this.reqUrl = reqUrl;
    }

    public static String getSign(Map<String,Object> map, String key){
        ArrayList<String> list = new ArrayList<String>();
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(null != entry.getValue() && !"".equals(entry.getValue())){
                if(entry.getValue() instanceof JSONObject) {
                    list.add(entry.getKey() + "=" + PayDigestUtil.getSortJson((JSONObject) entry.getValue()) + "&");
                }else {
                    list.add(entry.getKey() + "=" + entry.getValue() + "&");
                }
            }
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < size; i ++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result= result.substring(0,result.length()-1);
        result += "&key=" + key;
        _log.info("Sign Before MD5:" + result);
        result = PayDigestUtil.md5(result, encodingCharset).toUpperCase();
        _log.info("Sign Result:" + result);
        return result;
    }
}

